package com.lanou.sfu.songforu.fragment;

import android.app.Dialog;
import android.app.FragmentManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.sfu.songforu.activity.EnterActivity;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.music_bd.ManaMusic;
import com.lanou.sfu.songforu.music_bd.MusicActivityFragment;
import com.lanou.sfu.songforu.singleton.VolleySingleton;
import com.lanou.sfu.songforu.tools.MyConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by quyan on 15/10/21.
 */
public class MyFragment extends Fragment implements View.OnClickListener {
    private LinearLayout enter;
    private TextView user_name;
    private ImageView user_img;
    private ImageLoader imageLoader;
    private RelativeLayout music_bd, music_download, mv_download, mv_save, songs_save, songs_new;

    private MusicActivityFragment fragment;
    private FragmentManager manager;
    private List<ManaMusic> list = new ArrayList<ManaMusic>();
    private TextView songs_count,songs_new_title;

    private int mScreenWidth;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_layout_mine, null);

        // 初始化
        enter = (LinearLayout) view.findViewById(R.id.enter);
        enter.setOnClickListener(this);
        user_name = (TextView) view.findViewById(R.id.user_name);
        user_img = (ImageView) view.findViewById(R.id.user_img);

        music_bd = (RelativeLayout) view.findViewById(R.id.music_bd);
        music_bd.setOnClickListener(this);
        music_download = (RelativeLayout) view.findViewById(R.id.music_download);
        music_download.setOnClickListener(this);
        mv_download = (RelativeLayout) view.findViewById(R.id.mv_download);
        mv_download.setOnClickListener(this);
        mv_save = (RelativeLayout) view.findViewById(R.id.mv_save);
        mv_save.setOnClickListener(this);
        songs_save = (RelativeLayout) view.findViewById(R.id.songs_save);
        songs_save.setOnClickListener(this);
        songs_new = (RelativeLayout) view.findViewById(R.id.songs_new);
        songs_new.setOnClickListener(this);

        songs_count = (TextView) view.findViewById(R.id.songs_count);
        songs_new_title = (TextView) view.findViewById(R.id.songs_new_title);

        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        imageLoader = VolleySingleton.getInstance(getActivity()).getImageLoader();

        // 本地SDK数据查询
        Cursor cursor = getActivity().getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                null);

        songs_count.setText(cursor.getCount() + "首");

        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        mScreenWidth = metric.widthPixels;
    }

    @Override
    public void onResume() {
        super.onResume();
        String name = MyConfig.getSharePreStr(getActivity(), "name", "screen_name");
        String img = MyConfig.getSharePreStr(getActivity(), "img", "profile_image_url");

        if (name != null && !name.equals("0") && img != null && !img.equals("0")) {
            user_name.setText(name);
            ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(user_img, R.mipmap.ic_launcher,
                    R.mipmap.ic_launcher);
            imageLoader.get(img, imageListener);
        } else {
            user_name.setText(user_name.getText());
            user_img.setImageDrawable(user_img.getDrawable());
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.enter: // 登录页
                String name = MyConfig.getSharePreStr(getActivity(), "name", "screen_name");
                String img = MyConfig.getSharePreStr(getActivity(), "img", "profile_image_url");
                if (name != null && !name.equals("0") && img != null && !img.equals("0")) {

                } else {
                    Intent intent = new Intent(getActivity(), EnterActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.music_bd: // 本地音乐
                fragment = new MusicActivityFragment();
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, fragment).addToBackStack(null).commit();
                break;
            case R.id.music_download: // 歌曲下载
                String name1 = MyConfig.getSharePreStr(getActivity(), "name", "screen_name");
                String img1 = MyConfig.getSharePreStr(getActivity(), "img", "profile_image_url");
                if (name1 != null && !name1.equals("0") && img1 != null && !img1.equals("0")) {

                } else {
                    Intent intent = new Intent(getActivity(), EnterActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.mv_download: // MV下载
                String name2 = MyConfig.getSharePreStr(getActivity(), "name", "screen_name");
                String img2 = MyConfig.getSharePreStr(getActivity(), "img", "profile_image_url");
                if (name2 != null && !name2.equals("0") && img2 != null && !img2.equals("0")) {

                } else {
                    Intent intent = new Intent(getActivity(), EnterActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.mv_save: // 收藏的MV

                break;
            case R.id.songs_save: // 收藏的歌单

                break;
            case R.id.songs_new: // 点击新建的歌单
                    showDialog();
                break;

        }
    }



    private void showDialog() {
        View view = View.inflate(getActivity(), R.layout.music_new_dialog,null);
        view.setMinimumWidth(mScreenWidth - 40);
        final Dialog dialog = new Dialog(getActivity(), R.style.music_new_dialog);

        final Button okBtn = (Button) view.findViewById(R.id.ok_btn);
        final Button cancleBtn = (Button) view.findViewById(R.id.cancel_btn);
        final EditText music_tv = (EditText) view.findViewById(R.id.music_tv);


        View.OnClickListener btnListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v == okBtn) {
                    if (music_tv.getText() == null) {
                        songs_new_title.setText("点击新建歌单");
                        dialog.dismiss();
                    } else {
                        songs_new_title.setText(music_tv.getText());
                        dialog.dismiss();
                    }
                } else if  (v == cancleBtn) {
                    dialog.dismiss();
                }
            }
        };
        okBtn.setOnClickListener(btnListener);
        cancleBtn.setOnClickListener(btnListener);
        dialog.setContentView(view);
        dialog.show();
    }
}
